1
|
|
|
import App from "app"; |
2
|
|
|
const _ = App.libs._; |
3
|
|
|
|
4
|
|
|
class RestInterface { |
5
|
|
|
/** |
6
|
|
|
* @param {String} serviceName name of the service (used to emmit events) |
7
|
|
|
* @param {String} basepath |
8
|
|
|
*/ |
9
|
|
|
constructor(serviceName, basepath) { |
10
|
|
|
this.serviceName = serviceName; |
11
|
|
|
this.basepath = basepath; |
12
|
|
|
|
13
|
|
|
this.Request = App.ServicesContainer.get("AJAX"); |
14
|
|
|
this.EM = App.EventManager; |
15
|
|
|
|
16
|
|
|
this.collection = []; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get all itens |
21
|
|
|
* @method index |
22
|
|
|
* @param {Object} data querystring |
23
|
|
|
* @param {Function} onSuccess |
24
|
|
|
* @param {Function} onError |
25
|
|
|
* @return {null} |
26
|
|
|
*/ |
27
|
|
|
index(data, onSuccess, onError) { |
28
|
|
|
this.Request.send("get", this.basepath, data, onSuccess, onError); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Fetch single item |
33
|
|
|
* @param {String|Number} id |
34
|
|
|
* @param {Function} onSuccess |
35
|
|
|
* @param {Function} onError |
36
|
|
|
*/ |
37
|
|
|
show(id, onSuccess, onError) { |
38
|
|
|
this.Request.send("get", this.basepath + "/" + id, {}, onSuccess, onError); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new item |
43
|
|
|
* @param {Object} data |
44
|
|
|
* @param {Function} onSuccess |
45
|
|
|
* @param {Function} onError |
46
|
|
|
*/ |
47
|
|
|
store(data, onSuccess, onError) { |
48
|
|
|
this.Request.send( |
49
|
|
|
"post", |
50
|
|
|
this.basepath, |
51
|
|
|
data, |
52
|
|
|
(res, req) => { |
53
|
|
|
this.EM.notify(this.serviceName + ".create", res, req); |
54
|
|
|
|
55
|
|
|
if (typeof onSuccess === "function") { |
56
|
|
|
onSuccess(res, req); |
57
|
|
|
} |
58
|
|
|
}, |
59
|
|
|
(err, res, req) => { |
60
|
|
|
this.EM.notify(this.serviceName + ".create.error", err, res, req); |
61
|
|
|
|
62
|
|
|
if (typeof onError === "function") { |
63
|
|
|
onError(err, res, req); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param {Object} data |
71
|
|
|
* @param {Function} onSuccess |
72
|
|
|
* @param {Function} onError |
73
|
|
|
*/ |
74
|
|
|
update(data, onSuccess, onError) { |
75
|
|
|
let id = null; |
76
|
|
|
|
77
|
|
|
if (data.hasOwnProperty("id")) { |
78
|
|
|
id = data.id; |
79
|
|
|
} else { |
80
|
|
|
id = this._extractId(data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
const payload = _.merge(data, {_method: "PUT"}); |
84
|
|
|
this.Request.send( |
85
|
|
|
"post", |
86
|
|
|
this.basepath + "/" + id, |
87
|
|
|
payload, |
88
|
|
|
(res, req) => { |
89
|
|
|
this.EM.notify(this.serviceName + ".update", res, req); |
90
|
|
|
|
91
|
|
|
if (typeof onSuccess === "function") { |
92
|
|
|
onSuccess(res, req); |
93
|
|
|
} |
94
|
|
|
}, |
95
|
|
|
(err, res, req) => { |
96
|
|
|
this.EM.notify(this.serviceName + ".update.error", err, res, req); |
97
|
|
|
|
98
|
|
|
if (typeof onError === "function") { |
99
|
|
|
onError(err, res, req); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param {Object} data |
107
|
|
|
* @param {Function} onSuccess |
108
|
|
|
* @param {Function} onError |
109
|
|
|
*/ |
110
|
|
|
destroy(data, onSuccess, onError) { |
111
|
|
|
let id = null; |
112
|
|
|
if (data.hasOwnProperty("id")) { |
113
|
|
|
id = data.id; |
114
|
|
|
} else { |
115
|
|
|
id = this._extractId(data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
const payload = _.merge(data, {_method: "DELETE"}); |
119
|
|
|
this.Request.send( |
120
|
|
|
"post", |
121
|
|
|
this.basepath + "/" + id, |
122
|
|
|
payload, |
123
|
|
|
(res, req) => { |
124
|
|
|
this.EM.notify(this.serviceName + ".destroy", res, req); |
125
|
|
|
|
126
|
|
|
if (typeof onSuccess === "function") { |
127
|
|
|
onSuccess(res, req); |
128
|
|
|
} |
129
|
|
|
}, |
130
|
|
|
(err, res, req) => { |
131
|
|
|
this.EM.notify(this.serviceName + ".destroy.error", err, res, req); |
132
|
|
|
|
133
|
|
|
if (typeof onError === "function") { |
134
|
|
|
onError(err, res, req); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
export default RestInterface; |